unit fTestForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    btnPowerOff: TButton;
    btnReboot: TButton;
    btnLogOff: TButton;
    btnForceShutdown: TButton;
    btnExit: TButton;
    btnForceReboot: TButton;
    btnUsbRelayTest: TButton;
    procedure btnPowerOffClick(Sender: TObject);
    procedure btnRebootClick(Sender: TObject);
    procedure btnExitClick(Sender: TObject);
    procedure btnForceShutdownClick(Sender: TObject);
    procedure btnLogOffClick(Sender: TObject);
    procedure btnForceRebootClick(Sender: TObject);
    procedure btnUsbRelayTestClick(Sender: TObject);
  private
    procedure GetPrivilege;
    function SetPrivilege(sPrivilegeName: string; bEnabled: Boolean): Boolean;
  public
    function WinExit(iFlags: Integer): Boolean;
  end;

var
  Form1: TForm1;

implementation

uses fUsbRelayTest;

{$R *.dfm}

procedure TForm1.btnPowerOffClick(Sender: TObject);
begin
  GetPrivilege;
  ExitWindowsEx(EWX_POWEROFF, 0);
end;

procedure TForm1.btnRebootClick(Sender: TObject);
begin
  WinExit(EWX_REBOOT);
end;

procedure TForm1.btnUsbRelayTestClick(Sender: TObject);
begin
  frmUsbRelayTest := TfrmUsbRelayTest.Create(nil);
  frmUsbRelayTest.ShowModal;
  frmUsbRelayTest.Free;
end;

procedure TForm1.GetPrivilege;
const
  SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
var
  NewState: TTokenPrivileges;
  lpLuid: Int64;
  ReturnLength: DWord;
  ToKenHandle: Cardinal;
  tpResult: Boolean;
begin
  tpResult := OpenProcessToken(GetCurrentProcess,
                   TOKEN_ADJUST_PRIVILEGES
                   or TOKEN_ALL_ACCESS
                   or STANDARD_RIGHTS_REQUIRED
                   or TOKEN_QUERY,
                   ToKenHandle);
  if tpResult then
  begin
    tpResult := LookupPrivilegeValue(nil, SE_SHUTDOWN_NAME, lpLuid);
    NewState.PrivilegeCount := 1;
    NewState.Privileges[0].Luid := lpLuid;
    NewState.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
    ReturnLength := 0;
    if tpResult then
      AdjustTokenPrivileges(ToKenHandle, False, NewState, 0, nil, ReturnLength);
  end;
end;


function TForm1.SetPrivilege(sPrivilegeName: string; bEnabled: Boolean): Boolean;
var
  TPPrev, TP: TTokenPrivileges;
  Token: Cardinal;
  dwRetLen: DWord;
begin
  Result := False;
  OpenProcessToken(GetCurrentProcess,
                   TOKEN_ADJUST_PRIVILEGES
                   or TOKEN_QUERY,
                   Token);
  TP.PrivilegeCount := 1;
  if LookupPrivilegeValue(nil, PChar(sPrivilegeName), TP.Privileges[0].LUID) then
  begin
    if bEnabled then
      TP.Privileges[0].Attributes :=  SE_PRIVILEGE_ENABLED
    else
      TP.Privileges[0].Attributes := 0;
  end;
  dwRetLen := 0;
  Result := AdjustTokenPrivileges(
                        Token,
                        False,
                        TP,
                        SizeOf(TPPrev),
                        TPPrev,
                        dwRetLen);
  CloseHandle(Token);
end;

function TForm1.WinExit(iFlags: Integer): Boolean;
const
  SE_SHUTDOWN_PRIVILEGE = 'SeShutdownPrivilege';
begin
  Result := False;
  if not SetPrivilege(SE_SHUTDOWN_PRIVILEGE, True) then Exit;
  Result := ExitWindowsEx(iFlags, 0);
  SetPrivilege(SE_SHUTDOWN_PRIVILEGE, False);
end;

procedure TForm1.btnLogOffClick(Sender: TObject);
begin
  ExitWindowsEx(EWX_LOGOFF, 0);
end;

procedure TForm1.btnForceShutdownClick(Sender: TObject);
begin
  GetPrivilege;
  ExitWindowsEx(EWX_POWEROFF or EWX_FORCE,0);
end;

procedure TForm1.btnExitClick(Sender: TObject);
begin
  Application.Terminate;
end;

procedure TForm1.btnForceRebootClick(Sender: TObject);
begin
  WinExit(EWX_REBOOT or EWX_FORCE);
end;

end.
